
from django.db import models
from django.utils import timezone

from Apps.Project.models import Project
from Apps.User.models import User
from Apps.common.custommodel import CustomModel
from Apps.util.snowFlake import get_guid


class UiTestCat(CustomModel):
    label = models.CharField(max_length=40, verbose_name="UiTest模块名称", help_text="UiTest模块名称")
    index = models.CharField(max_length=20, default=get_guid, verbose_name="模块排序", null=True)
    parentId = models.CharField(max_length=20, verbose_name="上级模块", default="", null=True, blank=True)
    project = models.ForeignKey(to=Project, db_constraint=False, related_name="uitest_uitest_cat_project",
                                on_delete=models.SET_NULL, verbose_name="项目", null=True)

    class Meta:
        db_table = 'uitest_uitest_cat'
        verbose_name = db_table
        ordering = ('-index',)

    def __str__(self):
        return self.label


class UiTestCase(CustomModel):
    """
    录制脚本的存储
    """
    case_name = models.CharField(max_length=50, default="", verbose_name="用例名称")
    case_source_name = models.TextField(default="", verbose_name="上传的side文件原始名称，下载的时候用")
    case_url = models.CharField(max_length=255, default="", verbose_name="用例存储的url地址")
    case_data = models.TextField(default="", verbose_name="脚本的JSON原始数据")
    is_delete = models.BooleanField(default=False, verbose_name="用例是否删除")
    case_description = models.TextField(default="", verbose_name="用例描述")
    last_run_browser = models.CharField(max_length=50, default="chrome", verbose_name="最后执行用例的浏览器")
    STATUS_CHOICES = (
        (1, "未执行"),
        (2, "队列中"),
        (3, "正在执行"),
        (4, "成功"),
        (5, "失败"),
    )
    run_status = models.IntegerField(default=1, choices=STATUS_CHOICES, verbose_name="用例执行的状态")
    creator = models.ForeignKey(to=User, db_constraint=False, related_name="uitest_case_creator",
                                on_delete=models.SET_NULL, verbose_name="创建人", null=True)
    modifier = models.ForeignKey(to=User, db_constraint=False, related_name="uitest_case_modifier",
                                 on_delete=models.SET_NULL, verbose_name="修改人", null=True)
    cat = models.ForeignKey(to=UiTestCat, db_constraint=False, related_name="uitest_case_cat",
                            on_delete=models.SET_NULL, verbose_name="接口模块", null=True)
    project = models.ForeignKey(to=Project, on_delete=models.DO_NOTHING, db_constraint=False)

    class Meta:
        db_table = "uitest_case"
        verbose_name = db_table
        ordering = ('-create_time',)

    def __str__(self):
        return self.case_name


class UiTestCaseLog(CustomModel):
    """
    执行用例的日志
    """

    browserName = models.CharField(max_length=20, default="--", verbose_name="浏览器名称")
    platformName = models.CharField(max_length=20, default="--", verbose_name="PC 执行的平台")
    platform = models.CharField(max_length=20, default="ANY", verbose_name="移动端 执行的平台")
    session_id = models.CharField(max_length=50, default="", verbose_name="运行时的session_id")
    capabilities = models.JSONField(default=dict, verbose_name="对应每个执行节点的属性Json")
    run_case_data = models.TextField(default="", verbose_name="执行用例时，脚本的Json数据")
    run_case_data_tests = models.TextField(default="", verbose_name="测试步骤数据，用于前端展示用")
    current_test_id = models.CharField(max_length=50, default="", verbose_name="当前正在执行节点的ID")
    echo_text = models.JSONField(default=dict, verbose_name="测试过程中，echo输出的值")

    # 真正执行的时间，因为点击执行时，可能在排队等待执行
    start_time = models.DateTimeField(default=timezone.now, verbose_name="用例开始执行的时间")
    end_time = models.DateTimeField(default=timezone.now,  verbose_name="用例结束执行的时间")
    elapsed_time = models.CharField(max_length=16, default="", verbose_name="执行总耗时")
    case_url = models.CharField(max_length=255, default="", verbose_name="添加到执行列表后的对应地址")
    is_success = models.BooleanField(default=False, verbose_name="是否成功")
    STATUS_CHOICES = (
        (1, "未执行"),
        (2, "正在执行"),
        (3, "已执行"),
    )
    status = models.IntegerField(default=1, choices=STATUS_CHOICES,
                                 verbose_name="用例是否开始执行状态")
    run_message = models.JSONField(default=dict, blank=True, verbose_name="运行结果存储")
    img_url = models.JSONField(default=dict, verbose_name="每个步骤执行后，保存的图片地址")
    creator = models.ForeignKey(to=User, db_constraint=False, related_name="uitest_case_log_creator",
                                on_delete=models.SET_NULL, verbose_name="点击执行该用例的人", null=True)
    case = models.ForeignKey(to=UiTestCase, on_delete=models.CASCADE, db_constraint=False)
    project = models.ForeignKey(to=Project, on_delete=models.DO_NOTHING, db_constraint=False)

    class Meta:
        db_table = "uitest_case_log"
        verbose_name = db_table
        ordering = ('-create_time',)

    def __str__(self):
        return self.case
